#include <cstdlib>
#include <fstream>
#include "MPC.h"
#include "evaluator.h"

using namespace std;

/* threshold for performing cut */
int THRESHOLD[NUM_STAGES];

/* examine if the current node can be cut */
bool PerformCut(int alpha, int beta, int eval, int stage)
{
    if (eval - THRESHOLD[stage] >= beta)
        return true;
    if (eval + THRESHOLD[stage] <= alpha)
        return true;
    
    return false;
}

/* initialise the constants for MPC */
void InitialiseMPC()
{
    fstream filestr;
    
    filestr.open(MPC_FILE, fstream::in);
    
    if (!filestr.is_open())
    {
        for (int i = 0; i < NUM_STAGES; i++)
            THRESHOLD[i] = 10000000;
        return;
    }
    for (int i = 0; i < NUM_STAGES; i++)
    {
        char buffer[255];
        filestr.getline(buffer, 255);
        
        THRESHOLD[i] = atoi(buffer);
    }
    filestr.close();
}

/* write the threshold into file */
void WriteProbCut(double a[], double c[], double mean[], double deviation[])
{
    fstream filestr;
    
    filestr.open(MPC_FILE, fstream::out);
    
    if (!filestr.is_open())
        return;
        
    for (int i = 0; i < NUM_STAGES; i++)
    {
        THRESHOLD[i] = mean[i] + 2 * deviation[i];
        filestr << THRESHOLD[i] << endl;
    }
    filestr.close();
}
